<?php
/**
 * Description of ImportActionclass
 *
 * @author Mike
 */
class ImportAction extends Action{

/**
return import excel data
**/
public  function getExcel() {
        header("Content-Type:text/html; charset=utf-8");
        set_time_limit(0);
        
        import('@.ORG.PHPExcel.IOFactory','','.php');
        spl_autoload_register(array('Think','autoload'));   //必须重置，否则造成原TP类无法识别

        //处理上传文件
        if ($_FILES['upload']['name'] == '' || $_FILES['upload']['size'] <= 0)
            $this->error('请上传正确文件!');
        $ext = strtolower(substr($_FILES['upload']['name'], strrpos($_FILES['upload']['name'], '.') + 1));
        if ($ext != 'xls' && $ext != 'xlsx')
            $this->error('本功能仅支持excel');
        $filename = date('YmdHis') . '_product.xls';
        $status = move_uploaded_file($_FILES['upload']['tmp_name'], TEMP_PATH . $filename); //到temp目录
        if (!$status) {
            $this->error('上传失败，请重新上传');
            exit;
        }

        $inputFileName = TEMP_PATH . $filename;
        $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
        $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
        unlink(TEMP_PATH  . $filename); //上传后删除文件
        if (!$sheetData) {
            $this->error('上传数据失败或者上传的数据为空');
        }
        //$this->__saveProduct($sheetData);
        return $sheetData;
    }

//使用老类上传，暂不用
    public function simpleimport() {
        set_time_limit(0);
        import("@.ORG.Excel.reader");
        // ExcelFile($filename, $encoding);
        $data = new Spreadsheet_Excel_Reader();

        // Set output Encoding.
        $data->setOutputEncoding('gb2312');

          //$data->read('member2.xls');
        error_reporting(E_ALL ^ E_NOTICE);
        $product = M('Member'); //临时做member
        $dataProduct = array();
        for ($i = 1; $i <=$data->sheets[0]['numRows']; $i++) { //start from line 2
             $dataProduct['account'] = $data->sheets[0]['cells'][$i][2];
             $dataProduct['name'] = $data->sheets[0]['cells'][$i][3];
             $dataProduct['sex'] = $data->sheets[0]['cells'][$i][4];
             $dataProduct['password'] = md5($data->sheets[0]['cells'][$i][5]);
             $dataProduct['rank'] = $data->sheets[0]['cells'][$i][8];
             $dataProduct['succ_order'] = $data->sheets[0]['cells'][$i][9]; //消费次数
             $dataProduct['email'] = $data->sheets[0]['cells'][$i][12];
             $dataProduct['company'] = $data->sheets[0]['cells'][$i][13];
             $dataProduct['title'] = $data->sheets[0]['cells'][$i][14];
             $dataProduct['telephone'] = $data->sheets[0]['cells'][$i][15];
             $dataProduct['mobile'] = $data->sheets[0]['cells'][$i][16];
             $dataProduct['qq'] = $data->sheets[0]['cells'][$i][17];
             $dataProduct['address'] = $data->sheets[0]['cells'][$i][18];
             $dataProduct['zipcode'] = $data->sheets[0]['cells'][$i][19];
             $dataProduct['create_time'] = ($data->sheets[0]['cells'][$i][20] =="NULL")?0:strtotime($data->sheets[0]['cells'][$i][20] );
             $dataProduct['update_time'] = ($data->sheets[0]['cells'][$i][21] =="NULL")?0:strtotime($data->sheets[0]['cells'][$i][21] );
             dump($data->sheets[0]['cells'][$i][20]);
             $dataProduct['remark'] = $data->sheets[0]['cells'][$i][22];
             $dataProduct['payper_no'] = $data->sheets[0]['cells'][$i][23];
             
             
             //dump($dataProduct);
            $result = $product->add($dataProduct);
            
        }
        echo "导入成功！";
    }

    public function getCsv() {
        $row = 1;
        $handle = fopen("member.csv","r");
        $Member = M('Member');
        while ($data = fgetcsv($handle, 1000, ",")) {
            $row++;
            
             $dataProduct['account'] = $data[2];
             $dataProduct['name'] = $data[3];
             $dataProduct['sex'] = $data[4];
             $dataProduct['password'] = md5($data[5]);
             $dataProduct['rank'] = $data[8];//会员评级
             $dataProduct['succ_order'] = $data[9]; //消费次数
             $dataProduct['email'] = $data[12];
             $dataProduct['company'] = $data[13];
             $dataProduct['title'] = $data[14];
             $dataProduct['telephone'] = $data[15];
             $dataProduct['mobile'] = $data[16];
             $dataProduct['qq'] = $data[17];
             $dataProduct['address'] = $data[18];
             $dataProduct['zipcode'] = $data[19];
             $dataProduct['create_time'] = ($data[20] == "NULL")?0:strtotime($data[20]) ;
             $dataProduct['update_time'] = ($data[21] == "NULL")?0:strtotime($data[21]) ;
             $dataProduct['remark'] = $data[22];
             $dataProduct['payper_no'] = $data[23];
             $dataProduct['status'] = 1;
             //dump($dataProduct);
             $Member->add($dataProduct);

        }
        fclose($handle);
    }

}
?>
